package com.funky.controller;


import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.funky.common.annotation.UserLoginToken;
import com.funky.common.entity.Result;
import com.funky.common.entity.StatusCode;
import com.funky.pojo.Number;
import com.funky.pojo.User;
import com.funky.service.NumberService;
import com.funky.service.TokenService;
import com.funky.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaojin
 * @since 2021-01-08
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
@Api(tags = "用户管理相关接口")
public class UserController {

  @Autowired
  private UserService userService;

  @Autowired
  private NumberService numberService;

  @Autowired
  TokenService tokenService;

  @RequestMapping(value = "findAll", method = RequestMethod.GET)
  @ApiOperation("查询用户的接口")
  public Result findAllUser(){
    List<User> userList = userService.findAllUser();
    return new Result(true, StatusCode.OK, "查询成功",userList);
  }

  @RequestMapping(value = "register", method = RequestMethod.POST)
  @ApiOperation("用户注册的接口")
  @ApiImplicitParam(name = "user", value = "user类用户信息", paramType = "body", dataType= "User", required = true)
  public Result register(@RequestBody @Valid User user){
    boolean email = Validator.isEmail(user.getEmail());
    boolean re_email = userService.findByEmail(user.getEmail());
    if(email && re_email){
      Integer flag = userService.register(user);
      if (flag == 1){
        return new Result(true, StatusCode.OK, "注册成功",null);
      }
        return new Result(false, StatusCode.ERROR, "注册失败",null);
    }
    return new Result(false, StatusCode.ERROR, "邮箱格式不正确或邮箱已存在",null);
  }

  @RequestMapping(value = "login", method = RequestMethod.POST)
  @ApiOperation("用户登录的接口")
  @ApiImplicitParam(name = "user", value = "user类用户信息", paramType = "body", dataType= "User", required = true)
  @Transactional
  public Result login(@RequestBody @Valid User user){
    User u = userService.login(user);
    isNumber(u);
    if(ObjectUtil.isNotEmpty(u)){
      String token = tokenService.getToken(u);
      return new Result(true, StatusCode.OK, "登录成功",token);
    }
    return new Result(false, StatusCode.ERROR, "用户名或密码错误",null);
  }

  @UserLoginToken
  @GetMapping(value = "getMessage")
  public String getMessage(){
    return "你已通过验证";
  }

  //判断用户会员是否过期
  public void isNumber(User user) {
    if (user.getMember() == 1) {
      String memberId = user.getMemberId();
      Number number = numberService.getById(memberId);
      if (number.getEndTime().getTime() < new Date().getTime()) {
        //说明会员过期了，清除会员数据
        user.setMember(0);
        user.setMemberId("");
        UpdateWrapper<User> updateWrapper = new UpdateWrapper();
        userService.update(user,updateWrapper.eq("id", user.getId()));
        numberService.removeById(memberId);
      }
    }
  }
}
